/*
 ============================================================================
 Name        : Set_CGI.c
 Author      : liuzhanchun
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#if 1
#define SERVER_NAME cgiServerName
#endif
#if 0
#define SERVER_NAME "www.boutell.com"
#endif

/* You may need to change this, particularly under Windows;
	it is a reasonable guess as to an acceptable place to
	store a saved environment in order to test that feature.
	If that feature is not important to you, you needn't
	concern yourself with this. */

#ifdef WIN32
#define SAVED_ENVIRONMENT "c:\\cgicsave.env"
#else
#define SAVED_ENVIRONMENT "/tmp/cgicsave.env"
#endif /* WIN32 */

#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>
#include "config_ini.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>

char image_name[1000][256];
int image_num = 0;

//搜索 指定目录下的所有文件及其子目录下的文件
void getFileName(char * dirPath)
{
  DIR *dir=opendir(dirPath);
  if(dir==NULL)
  {
  printf("%s\n",strerror(errno));
  return;
  }
  chdir(dirPath);//进入到当前读取目录
  struct dirent *ent;
  while((ent=readdir(dir)) != NULL)
  {
  if(strcmp(ent->d_name,".")==0||strcmp(ent->d_name,"..")==0)
  {
   continue;
  }
  int size = strlen(ent->d_name); 
  if(strcmp( ( ent->d_name + (size - 4) ) , ".jpg") != 0)  //只存取.gmm 扩展名的文件名 
   continue;
   
  struct stat st;
  stat(ent->d_name,&st);
  if(S_ISDIR(st.st_mode))  //如果是子目录，继续递归搜索
  {
   getFileName(ent->d_name);
  }
  else
  {
   //printf("%s\n",ent->d_name);
   strcpy(image_name[image_num], ent->d_name);

//sprintf("")
   image_num++;
  }
  }
  closedir(dir);
  chdir("..");//返回当前目录的上一级目录
}


int cgiMain() {
	char local_ip[32] = {0};
    char remote_ip[32] = {0};
    char remote_port[32] = {0};
	char curl_cmd[1024];

		   // configFileStrGet("/boa/my.ini","local_ip",local_ip,32);
		   // configFileStrGet("/boa/my.ini","remote_ip",remote_ip,32);
		   // configFileStrGet("/boa/my.ini","remote_port",remote_port,32);



			//sprintf(curl_cmd,"/usr/bin/curl http://%s/cgi-bin/UVSSapi.cgi?%7B%22cmd%22:%22get%22,%22key%22:%22test%20image%22%7D",local_ip);
			//fprintf(cgiOut,"%s\n",curl_cmd);
			//printf("%s",curl_cmd);
			          system("/usr/bin/curl http://127.0.0.1/cgi-bin/UVSSapi.cgi?%7B%22cmd%22:%22get%22,%22key%22:%22test%20image%22%7D &");
	/**/

	/* Send the content type, letting the browser know this is HTML */
	cgiHeaderContentType("text/html; charset=UTF-8");

	/* Top of the page */
	fprintf(cgiOut, "<HTML><HEAD>\n");
	fprintf(cgiOut, "<TITLE>web 管理</TITLE></HEAD>\n");
	fprintf(cgiOut, "<BODY><H1></H1>\n");

	//刷新页面
	fprintf(cgiOut, "<meta http-equiv=Refresh content=0;URL=../manager.html\n");
	fprintf(cgiOut, "</BODY></HTML>\n");

	return 0;
}

